package org.bakerstreet.components.render;

import java.util.List;

import org.bakerstreet.components.ComboBox;
import org.bakerstreet.components.Component;

public class ComboboxRender extends ComponentRender {

	@Override
	public void render(Component comp, StringBuffer sb) {
		ComboBox combo = (ComboBox) comp;
		List<String> options = combo.getOptions();
				
		
		String containerId = "$"+comp.getRow()+"$"+comp.getColumn();
		
		sb.append("comp"+comp.getId()+" = new ComboBox(\""+comp.getId()+"\",\""+containerId+"\");");
		
		for (String option : options) {
			sb.append("comp"+comp.getId()+".addOption(\""+option+"\");");
		}
		
		sb.append("comp"+comp.getId()+".render();");
		
		sb.append("comp"+comp.getId()).append(".setValue(\"").append(combo.getValue()).append("\");");
	}

	@Override
	public void update(Component comp, StringBuffer sb) {
		ComboBox combo = (ComboBox) comp;
		
		String jsComponent = "comp"+comp.getId(); 
		sb.append(jsComponent).append(".setValue(\"").append(combo.getValue()).append("\");");
		sb.append(jsComponent).append(".setEnabled(").append(comp.isEnabled()).append(");");
	}

}
